package com.rss.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;


import android.util.Xml;

public class XmlParser {
	
	public static final String ns = null;

	public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException
	{
		try
		{
		XmlPullParser parser = Xml.newPullParser();
		parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
		parser.setInput(in, "utf-8");
		parser.nextTag();
		
		return readFeed(parser);
		}
		finally
		{
			in.close();
		}
	}
	
	private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException
	{
		List<Entry> entrise = new ArrayList<Entry>();
		
		parser.require(XmlPullParser.START_TAG, ns, "rss");
		parser.next();
		parser.next();
		while(parser.next() != XmlPullParser.END_TAG)
		{
			if (parser.getEventType() != XmlPullParser.START_TAG) {
	            continue;
	        }
			
			String name = parser.getName();
			
			if(name.equals("item"))
			{
				entrise.add(readEntry(parser));
			}
			else
			{
				skip(parser);
			}
		}
		return entrise;
	}
	
	public static class Entry
	{
		public final String link;
		public final String title;
		public final String pudDate;
		
		private Entry(String url, String title, String pudDate)
		{
			this.pudDate=pudDate;
			this.link = url;
			this.title = title;
			
		}
	}
		private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException
		{
			parser.require(XmlPullParser.START_TAG, ns, "item");
			
			String pudDate = null;
			String link = null;
			String title = null;
			
			while(parser.next() != XmlPullParser.END_TAG)
			{
				if(parser.getEventType() != XmlPullParser.START_TAG)
				{
					continue;
				}
				String name = parser.getName();
				if(name.equals("title"))
				{
					title = readTitle(parser);
				}
				else
				{
					if(name.equals("link"))
					{
						link = readLink(parser);
					}
					else
					{
						if(name.equals("pubDate"))
						{
							pudDate = readPudDate(parser);
						}
						else
						{
							skip(parser);
						}
					}
				}
			}
			return new Entry(link,title,pudDate);
		}
		
		private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException
		{
			parser.require(XmlPullParser.START_TAG, ns, "title");
			String title = readText(parser);
			parser.require(XmlPullParser.END_TAG, ns, "title");
			return title;
		}
		
		private String readPudDate(XmlPullParser parser) throws IOException, XmlPullParserException
		{
			parser.require(XmlPullParser.START_TAG, ns, "pubDate");
			String pudDate =readText(parser);
			parser.require(XmlPullParser.END_TAG, ns, "pubDate");
			return pudDate;
		}
		
		private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException
		{
			parser.require(XmlPullParser.START_TAG, ns, "link");
			String url = readText(parser);
			parser.require(XmlPullParser.END_TAG, ns, "link");
			return url;
		}
		
		private String readText(XmlPullParser parser) throws IOException, XmlPullParserException
		{
			String result = "";
			if (parser.next() == XmlPullParser.TEXT)
			{
				result = parser.getText();
				parser.nextTag();
			}
			return result;
		}
		
		private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
		    if (parser.getEventType() != XmlPullParser.START_TAG) {
		        throw new IllegalStateException();
		    }
		    int depth = 1;
		    while (depth != 0) {
		        switch (parser.next()) {
		        case XmlPullParser.END_TAG:
		            depth--;
		            break;
		        case XmlPullParser.START_TAG:
		            depth++;
		            break;
		        }
		    }
		 }
	}
	
	

